﻿#region << 文 件 说 明 >>

/*----------------------------------------------------------------
// 文件名称：VampirewalDialog
// 创 建 者：杨程
// 创建时间：2021/10/18 10:51:10
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//
//
//----------------------------------------------------------------*/

#endregion

namespace Vampirewal.Core.Components;

/// <summary>
/// Vampirewal自定义消息弹出框
/// </summary>
internal sealed class VampirewalCoreDialogService : IVampirewalCoreDialogMessage
{
    /// <summary>
    /// 弹出PopupWindow消息框
    /// </summary>
    public void ShowPopupWindow(PopupWindowSetting setting)
    {
        var Popup = VampirewalCoreContext.GetInstance().GetView<IPopupWindow>("PopupWindow");
        Popup.Setting(setting);

        Popup.Show();
    }

    /// <summary>
    /// 弹出DialogWindow窗体
    /// </summary>
    /// <param name="setting">窗体设置</param>
    public object OpenDialogWindow(DialogWindowSetting setting)
    {
        var Dialog = VampirewalCoreContext.GetInstance().GetView<IDialogWindow>("ShowDialogWindow");
        WindowsManager.GetInstance().RegisterDialogWindow(Dialog.ViewId, Dialog);
        Dialog.Setting(setting);
        Dialog.ShowDialog();

        var vm = Dialog.DataContext as ViewModelBase;

        return vm.GetResult();
    }

    /// <summary>
    /// 弹出正常Windows窗体
    /// </summary>
    /// <param name="setting"></param>
    /// <returns></returns>
    public object OpenNormalWindow(DialogWindowSetting setting)
    {
        var Dialog = VampirewalCoreContext.GetInstance().GetView<IDialogWindow>("ShowDialogWindow");
        WindowsManager.GetInstance().RegisterDialogWindow(Dialog.ViewId, Dialog);
        Dialog.Setting(setting);
        //Dialog.Topmost = true;
        Dialog.Activate();
        Dialog.Show();

        var vm = Dialog.DataContext as ViewModelBase;

        return vm.GetResult();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="defaultPath"></param>
    /// <returns></returns>
    public string OpenVampirewalFolderBrowserDialog(string defaultPath="")
    {
        var Dialog = VampirewalCoreContext.GetInstance().GetView<IFolderPickerWindow>("FolderPicker");

        Dialog.LoadDirectory(defaultPath);

        if(Dialog.ShowDialog()== true)
        {
            return Dialog.SelectedPath;
        }


        return "";
    }

    /// <summary>
    /// 文件单选弹窗
    /// </summary>
    /// <param name="Title">标题</param>
    /// <param name="Filter">过滤要选择的文件类型</param>
    /// <param name="InitialDirectory">设置打开文件对话框的默认路径</param>
    /// <returns></returns>
    public OpenFileDialogResult OpenSingleFileDialog(string Title, string Filter = "", string InitialDirectory = "")
    {
        VampirewalCoreContext.GetInstance().GetContext("OpenFileDialog", out object DialogType);

        if (DialogType is Type type)
        {
            dynamic dialog = Activator.CreateInstance(type);

            dialog.Title = Title;
            dialog.Filter = Filter;
            dialog.InitialDirectory = InitialDirectory;
            dialog.Multiselect = false;

            if (dialog.ShowDialog() == true)
            {
                OpenFileDialogResult result = new OpenFileDialogResult()
                {
                    FileName = dialog.FileName,
                    SafeFileName = dialog.SafeFileName
                };

                return result;
            }
        }

        return new OpenFileDialogResult();
    }

    /// <summary>
    /// 文件多选弹窗
    /// </summary>
    /// <param name="Title">标题</param>
    /// <param name="Filter">过滤要选择的文件类型</param>
    /// <param name="InitialDirectory">设置打开文件对话框的默认路径</param>
    /// <returns></returns>
    public List<OpenFileDialogResult> OpenMultiselectFileDialog(string Title, string Filter = "", string InitialDirectory = "")
    {
        VampirewalCoreContext.GetInstance().GetContext("OpenFileDialog", out object DialogType);

        if (DialogType is Type type)
        {
            dynamic dialog = Activator.CreateInstance(type);

            dialog.Title = Title;
            dialog.Filter = Filter;
            dialog.InitialDirectory = InitialDirectory;
            dialog.Multiselect = true;

            if (dialog.ShowDialog() == true)
            {
                List<OpenFileDialogResult> list = new List<OpenFileDialogResult>();

                int SelectedFileCount = dialog.FileNames.Count();

                for (int i = 0; i < SelectedFileCount; i++)
                {
                    OpenFileDialogResult result = new OpenFileDialogResult()
                    {
                        FileName = dialog.FileNames[i],
                        SafeFileName = dialog.SafeFileNames[i]
                    };

                    list.Add(result);
                }

                return list;
            }
        }

        return null;
    }

    /// <summary>
    /// 弹出Notify通知窗
    /// </summary>
    public void OpenNotifyWindow(NotifyWindowSetting Setting)
    {
        var notify = VampirewalCoreContext.GetInstance().GetView<INotifyWindow>("NotifyWindow");
        notify.Setting(Setting);
        notify.Topmost = true;
        notify.ShowInTaskbar = false;
        //notify.Owner = WindowsManager.GetInstance().MainWindow;
        notify.ShowDialog();
    }

    

}

/// <summary>
/// 文件选择器返回值
/// </summary>
public struct OpenFileDialogResult
{
    /// <summary>
    /// 文件全路径
    /// </summary>
    public string FileName { get; set; }
    /// <summary>
    /// 仅文件名和扩展名
    /// </summary>
    public string SafeFileName { get; set; }
}